package com.project.uitl;

import com.project.service.Servlet;

import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class ServletFactory {

    private static Properties pro = new Properties();

    private static Map<String, Servlet> servletMap = new HashMap<>();

    static {
        try {
            pro.load(new FileReader("Service.txt"));
            Set<String> keySet = pro.stringPropertyNames();
            for (String key : keySet
            ) {
                String classPath = pro.getProperty(key);
                Class c = Class.forName(classPath);
                servletMap.put(key, (Servlet) c.getConstructor().newInstance());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static Servlet getService(String url) {
//        String classPath = pro.getProperty(url);
//        if (classPath == null) {
//            return null;
//        }
        return servletMap.get(url);
    }
}
